package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Officialstu;
import cn.wolfcode.crm.domain.StudentState;
import cn.wolfcode.crm.mapper.OfficialstuMapper;
import cn.wolfcode.crm.mapper.StudentStateMapper;
import cn.wolfcode.crm.query.QueryForObject;
import cn.wolfcode.crm.service.IStudentStateService;
import cn.wolfcode.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class StudentStateServiceImpl implements IStudentStateService {
    @Autowired
    private StudentStateMapper mapper;
    @Autowired
    private OfficialstuMapper officialstuMapper;

    public void deleteByPrimaryKey(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    public void insert(StudentState record) {
        mapper.insert(record);

    }

    public StudentState selectByPrimaryKey(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    public List<StudentState> selectAll() {
        return mapper.selectAll();
    }

    public void updateByPrimaryKey(StudentState record) {
        mapper.updateByPrimaryKey(record);
    }

    public PageResult query(QueryForObject qo) {
        int total = mapper.queryForTotal(qo);
        if (total == 0) {
            return new PageResult();
        }

        List<StudentState> rows = mapper.queryForList(qo);
        return new PageResult(total, rows);
    }

    @Override
    public void save(StudentState studentState) {
        //查到对应的学员信息,封装到转班信息对象中
        Officialstu officialstu = officialstuMapper.selectByPrimaryKey(studentState.getStudent().getId());
        studentState.setTotalAmount(officialstu.getTotalFee());//总金额
        studentState.setHasPay(officialstu.getPaidFee());//已付金额
        studentState.setNonePay(officialstu.getNoPay());//未付金额
        studentState.setOldGrade(officialstu.getIttclass());//以前的班级
        studentState.setPayee(officialstu.getMarketer());//营销人员
        studentState.setTel(officialstu.getTel());//联系电话
        studentState.setTime(new Date());//转班时间
        //保存转班信息
        mapper.insert(studentState);
        //改变学员状态为  2转班中
        officialstuMapper.changeState(officialstu.getId(),2);
    }

    public void checkState(Long id) {
//        改变自身的状态值
        mapper.check(id);

        StudentState studentState = mapper.selectByPrimaryKey(id);
        Officialstu officialstu = new Officialstu();

//        转班审核后改变正式学员的状态和其班级信息
        officialstuMapper.changeClasses(studentState.getStudent().getId(), studentState.getNewGrade().getId(), 1);
    }
}
